package com.example.dbdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.PeriodicSync;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private DBOpenHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        helper = new DBOpenHelper(this);
        //SQLiteDatabase db = helper.getWritableDatabase();

    }

    public void findAll(View view) {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from info ", null);
        List<Person> person = new ArrayList<Person>();
        while (cursor.moveToNext()) {
            Person person1 = new Person();
            person1.setId(cursor.getInt(0));
            person1.setName(cursor.getString(1));
            person1.setPhone(cursor.getString(2));
            person.add(person1);
            person1 = null;
        }
        cursor.close();
        for (Person p : person){
            System.out.println(p.toString());
        }
    }

    public void add(View view){
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("insert into info (name,phone) values(?,?)",new Object[]{"张三","8855"});
        Toast.makeText(this,"添加成功",0).show();
    }
}